package com.tech15.core.action;

import static javacommon.util.extjs.Struts2JsonHelper.outJson;
import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.CtgData;
import javacommon.util.LoginInfo;
import javacommon.util.Notes;
import javacommon.util.ResInfo;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.util.HttpUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.core.model.SysUser;
import com.tech15.core.service.ModuleManager;
import com.tech15.core.service.SysUserManager;
import com.tech15.core.vo.query.SysUserQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class SysUserAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "realname asc"; 
	
	//forward paths
	protected static final String LIST_JSP = "/sys/user_list.jsp";
	protected static final String CREATE_JSP = "/sys/user_add.jsp";
	protected static final String EDIT_JSP = "/sys/user_edit.jsp";
	protected static final String SHOW_JSP = "/sys/user_show.jsp";
	protected static final String VIEW_JSP = "/sys/user_view.jsp";
	protected static final String USER_UPDATE_PWD = "/sys/user_update_pwd.jsp";
	protected static final String USER_SETTING = "/sys/user_setting.jsp";
	protected static final String CHECKPERLIST_JSP= "/sys/user_permission_list.jsp";
	protected static final String SHOWPERLIST_JSP= "/sys/user_permission_show_list.jsp";
	private SysUserManager sysUserManager;
	private ModuleManager moduleManager;
	
	private SysUser sysUser;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			sysUser = new SysUser();
		} else {
			sysUser = (SysUser)sysUserManager.getById(id);
		}
	}
	
	public void setSysUserManager(SysUserManager manager) {
		this.sysUserManager = manager;
	}	
	public void setModuleManager(ModuleManager manager) {
		this.moduleManager = manager;
	}
	
	public Object getModel() {
		return sysUser;
	}
	
	public void setUserid(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 获取模块列表 查看用户权限 */
	public String showPer() {
		List<CtgData> list = moduleManager.userFinalModuleTree(id);
		getRequest().setAttribute("list", list);
		return SHOWPERLIST_JSP;
	}
	/** 获取模块列表 设置用户权限 */
	public String checkPer() {
		List<CtgData> list = moduleManager.findAuthModuleTree(id,"1",Notes.APPID);
		getRequest().setAttribute("list", list);
		return CHECKPERLIST_JSP;
	}
	
	public void searchUser(){
		Map map = new HashMap();
		String realname = "";
		try {
			if(!isNullOrEmptyString(key)){
				realname = new String(key.getBytes("ISO-8859-1"),"UTF-8");
			}
			List<SysUser> list = sysUserManager.selectUser(keyid,key);
			map.put("totalRecords", 0);
			map.put("root", list);
			outJson(map);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public void searchLeader(){
		Map map = new HashMap();
		String realname = "";
		try {
			if(!isNullOrEmptyString(key)){
				realname = new String(key.getBytes("ISO-8859-1"),"UTF-8");
			}
			realname = key;
			List<SysUser> list = sysUserManager.searchLeader(keyid,key,orgType);
			map.put("totalRecords", 0);
			map.put("root", list);
			outJson(map);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public void selectOnlyUser(){
		Map map = new HashMap();
		String realname = "";
		try {
			if(!isNullOrEmptyString(key)){
				realname = new String(key.getBytes("ISO-8859-1"),"UTF-8");
			}
			realname = key;
			List<SysUser> list = sysUserManager.selectOnlyUser(key);
			map.put("totalRecords", 0);
			map.put("root", list);
			outJson(map);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	/** 执行搜索 */
	public String list() {
		SysUserQuery query = newQuery(SysUserQuery.class,DEFAULT_SORT_COLUMNS);
		LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
		Page page = sysUserManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	/**查看对象*/
	public String view() {
		return VIEW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public void saveAdmin() {
		Map<String, Object> resInfo = null;
		try{
			boolean mark = sysUserManager.saveAdminUser(sysUser);
			if(mark){
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}else{
				resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败，用户名重复！","");
			}
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/** 保存新增对象 */
	public void save() {
		LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
		Map<String, Object> resInfo = null;
		try{
			boolean mark = sysUserManager.saveUser(sysUser);
			if(mark){
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}else{
				resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败，用户名重复！","");
			}
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			boolean mark = sysUserManager.updateUser(this.sysUser);
			if(mark){
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}else{
				resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败，用户名重复！","");
			}
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			sysUserManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	/**进入用户密码修改页*/
	public String settingPwd(){
		
		return USER_UPDATE_PWD;
	}
	
	public String setting(){
		setSysUser(sysUserManager.getById(getLoginInfo().getUserid()));
		return USER_SETTING;
	}
	/**修改当前密码*/
	public void updatePassword(){
		Map<String, Object> resInfo = null;
		try {
			LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
			boolean mark = sysUserManager.updatePassword(loginInfo.getUserid(), pwd, newpwd,this.sysUser);
			if(mark){
				getRequest().getSession().removeAttribute(Notes.LOGIN_INFO);
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}else{
				resInfo = ResInfo.getResInfo("",false, false, 2,this.OPERATE_FAILED+"，当前密码有误！","");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	/**批量对象*/
	public void batchStartActive() {
		Map<String, Object> resInfo = null;
		try {
			for(int i = 0; i < items.length; i++) {
				Hashtable params = HttpUtils.parseQueryString(items[i]);
				java.lang.Long id = (Long) params.get("username");
				sysUserManager.activeStartUser(id);
			}
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	/**批量对象*/
	public void batchDisableActive() {
		Map<String, Object> resInfo = null;
		try {
			for(int i = 0; i < items.length; i++) {
				Hashtable params = HttpUtils.parseQueryString(items[i]);
				java.lang.Long id = (Long) params.get("username");
				sysUserManager.activeDisableUser(id);
			}
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	
	/**修改当前密码*/
	public void checkWsByDept(){
		Map<String, Object> resInfo = null;
		try {
			boolean mark = sysUserManager.checkWsByDept(deptId);
			if(mark){
				getRequest().getSession().removeAttribute(Notes.LOGIN_INFO);
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}else{
				resInfo = ResInfo.getResInfo("",false, false, 2,this.OPERATE_FAILED+"，当前密码有误！","");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	private String did;
	private String pwd;
	private String newpwd;
	private Long keyid;
	private String key;
	private Long deptId;
	private Integer orgType;

	public Integer getOrgType() {
		return orgType;
	}

	public void setOrgType(Integer orgType) {
		this.orgType = orgType;
	}

	public Long getDeptId() {
		return deptId;
	}

	public void setDeptId(Long deptId) {
		this.keyid = deptId;
		this.deptId = deptId;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Long getKeyid() {
		return keyid;
	}

	public void setKeyid(Long keyid) {
		this.keyid = keyid;
	}

	public String getNewpwd() {
		return newpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getDid() {
		return did;
	}

	public void setDid(String did) {
		this.did = did;
	}

	public SysUser getSysUser() {
		return sysUser;
	}

	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}

}
